package com.uaroadshow.entity.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.uaroadshow.entity.trade.Order;
import com.uaroadshow.entity.trade.Product;

/**
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name= "user_course")
@JsonIgnoreProperties(value = { "hibernate_lazy_initializer", "handler", "fieldHandler" })
public class UserCourse {
    
    @Id
    @GeneratedValue
    private Long id;
    
    private String code;
    
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Product product;

    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    @JsonIdentityReference(alwaysAsId = true)
    private User user;
    
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    @JsonIdentityReference(alwaysAsId = true)
    private Order order;
    
    @Column(name = "is_used")
    private boolean used;
    
    /** 已使用课程 */
    private String usedCode;
    
    /** 创建时间 */
    private Date createTime;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public UserCourse() {
    }
    
    /**
     * @param order
     */
    public UserCourse(Order order) {
        this.product = order.getProduct();
        this.user = order.getUser();
        this.order = order;
        this.code = order.getProduct().getCode();
        this.createTime = new Date();
        this.disabled = false;
    }
    
    /**
     * @return
     */
    public UserCourse verificate() {
        this.used = true;
        this.usedCode = this.code;
        return this;
    }
    
    /**
     * @param used
     * @param code
     * @return
     */
    public UserCourse verificate(boolean used, String code) {
        this.used = used;
        this.usedCode = code;
        return this;
    }
    
    /**
     * @return
     */
    @Transient
    public List<String> getUsedCodeList() {
        if (StringUtils.isBlank(usedCode)) {
            return new ArrayList<String>();
        }
        String[] codeList = StringUtils.split(usedCode, "|");
        return Stream.of(codeList).collect(Collectors.toList());
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the code
     */
    public String getCode() {
        return code;
    }

    /**
     * @return the product
     */
    public Product getProduct() {
        return product;
    }

    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * @return the order
     */
    public Order getOrder() {
        return order;
    }

    /**
     * @return the used
     */
    public boolean isUsed() {
        return used;
    }
    
    /**
     * @return
     */
    public String getUsedCode() {
        return usedCode;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param code the code to set
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * @param product the product to set
     */
    public void setProduct(Product product) {
        this.product = product;
    }

    /**
     * @param user the user to set
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * @param order the order to set
     */
    public void setOrder(Order order) {
        this.order = order;
    }

    /**
     * @param used the used to set
     */
    public void setUsed(boolean used) {
        this.used = used;
    }
    
    /**
     * @param usedCode
     */
    public void setUsedCode(String usedCode) {
        this.usedCode = usedCode;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

}